//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.crash;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.tuliu.appupdate.crash.MultipartUtility.UploadCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FilenameFilter;

public class CrashReportManager {
    private static final String CRASH_LAST_REPORT = "crash_last_report";
    private static final String KEY_CRASH_LAST_REPORT = "key_crash_last_report";
    private static final long CRASH_REPORT_INTERVAL = 43200000L;
    private AsyncTask<Void, Void, Void> mTask;
    private Context mContext;
    private boolean hasRunning;
    private static FilenameFilter sNewLogFilter = new FilenameFilter() {
        public boolean accept(File dir, String filename) {
            File file = new File(dir, filename);
            return file.exists() && file.isFile() && file.length() > 0L && filename.endsWith(".newlog");
        }
    };

    public CrashReportManager(Context context) {
        this.mContext = context;
    }

    public void postCrashFile() {
        if(!this.hasRunning && this.shouldReportCrash(this.mContext) && this.hasCrashLog(this.mContext)) {
            this.hasRunning = true;
//            this.mTask = (new AsyncTask() {
//                protected Void doInBackground(Void... params) {
//                    File logDir = new File(CrashReportManager.this.mContext.getFilesDir().getParent(), "UpdateLib/CrashLog");
//                    if(logDir.exists() && logDir.isDirectory()) {
//                        CrashReportManager.this.postByHttpUrlConnection(CrashReportManager.this.mContext, logDir);
//                    }
//
//                    return null;
//                }
//
//                protected void onPostExecute(Void aVoid) {
//                    CrashReportManager.this.hasRunning = false;
//                }
//            }).execute(new Void[0]);
        }

    }

    public void cancel() {
        if(this.mTask != null) {
            this.mTask.cancel(true);
        }

    }

    private boolean hasCrashLog(Context c) {
        if(c == null) {
            return false;
        } else {
            try {
                File e = new File(c.getFilesDir().getParent(), "UpdateLib/CrashLog");
                File[] logs = e.listFiles(sNewLogFilter);
                return logs != null && logs.length > 0;
            } catch (Exception var4) {
                return false;
            }
        }
    }

    private void postByHttpUrlConnection(Context context, File logDir) {
        try {
            this.postNew(logDir, context);
        } catch (Throwable var4) {
            var4.printStackTrace();
        }

    }

    private boolean shouldReportCrash(Context context) {
        SharedPreferences sp = context.getSharedPreferences("key_crash_last_report", 0);
        long lastReport = sp.getLong("crash_last_report", 0L);
        long curTime = System.currentTimeMillis();
        return curTime - lastReport >= 43200000L;
    }

    private boolean deleteLogFile() {
        File logDir = new File(this.mContext.getFilesDir().getParent(), "UpdateLib/CrashLog");
        File[] logs = logDir.listFiles(sNewLogFilter);
        if(logs != null && logs.length > 0) {
            File[] arr$ = logs;
            int len$ = logs.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                File file = arr$[i$];
                file.delete();
            }

            return true;
        } else {
            return false;
        }
    }

    private void onReportFinished(Context context, File des, boolean isSuccess) {
        if(isSuccess) {
            if(des.exists()) {
                des.delete();
            }

            this.deleteLogFile();
            SharedPreferences sp = context.getSharedPreferences("key_crash_last_report", 0);
            sp.edit().putLong("crash_last_report", System.currentTimeMillis()).commit();
        }

        this.hasRunning = false;
    }

    private static String getSendErrorLogUrl(Context context) {
        return "http://openbox.mobilem.360.cn/intf/getErrorLog?sign=" + Md5Utils.md5(DeviceUtils.getIMEI2(context) + "errorlog").toLowerCase() + "&m=" + DeviceUtils.getIMEI(context) + "&m2=" + DeviceUtils.getIMEI2(context) + "&v=" + DeviceUtils.getVersionName(context) + "&vc=" + DeviceUtils.getVersionCode(context);
    }

    private void postNew(File logDir, Context context) {
        try {
            final File[] e = logDir.listFiles(sNewLogFilter);
            String urlNew = getSendErrorLogUrl(context);
            if(e != null && e.length > 0) {
                MultipartUtility mRequest = new MultipartUtility(urlNew, new UploadCallback() {
                    public void onFinish(String response, boolean success) {
                        boolean jsonSuccess = false;
                        JSONObject object = null;

                        try {
                            object = new JSONObject(response);
                        } catch (JSONException var6) {
                            var6.printStackTrace();
                        }

                        if(object != null && object.optString("errno", "-1").equals("0")) {
                            jsonSuccess = true;
                        }

                        CrashReportManager.this.onReportFinished(CrashReportManager.this.mContext, e[0], success && jsonSuccess);
                    }
                });

                try {
                    mRequest.upload(e[0], "data=");
                } catch (Exception var11) {
                    var11.printStackTrace();
                } finally {
                    mRequest.finish();
                }
            }
        } catch (Throwable var13) {
            var13.printStackTrace();
        }

    }
}
